using cframework_xlua;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEngine;

public static class InitFrameworkTool
{
    [MenuItem("cframework_xlua/InitFramework")]
    public static void InitFramework()
    {
        var settingGuids = AssetDatabase.FindAssets("load_dll_config t:textasset");
        if(settingGuids.Length <= 0)
        {
            throw new System.Exception("load_dll_config not found");
        }

        AddressableAssetSettingsDefaultObject.GetSettings(true);

        var configPath = AssetDatabase.GUIDToAssetPath(settingGuids[0]);
        var config = Newtonsoft.Json.JsonConvert.DeserializeObject<LoadDllConfig>(AssetDatabase.LoadAssetAtPath<TextAsset>(configPath).text);

        var b = EditorBuildSettings.scenes.ToList();

        if(b.Count <= 0)
        {
            throw new System.Exception("EditorBuildSettings.scenes is empty");
        }

        


        var scene = b.Find(a => { return a.path == config.launchHotUpdateScene; });
        if(scene == null)
        {
            scene = new EditorBuildSettingsScene(config.launchHotUpdateScene, true);
        }
        else
        {
            b.Remove(scene);
        }
        scene.enabled = true;
        b.Insert(0, scene);


        if (b.Count <= 1)
        {
            throw new System.Exception("EditorBuildSettings.scenes don't contain game scene");
        }

        var firstGameScenePath = b[1].path;

        config.gameScene = firstGameScenePath;
        CommonUtil.ObjectToJsonFile(config, configPath);

        //var option = EditorUtility.DisplayDialogComplex("", "change load method of first game scene?", "from Addressable", "cancel", "from SceneManager");
        //switch(option)
        //{
        //    case 0:
        //        {
        //            b[1].enabled = false;
        //            AddressableTool.EnableAssetAddressable(firstGameScenePath);
        //        }
        //        break;
        //    case 2:
        //        {
        //            b[1].enabled = true;
        //            AddressableTool.DisableAssetAddressable(firstGameScenePath);
        //        }
        //        break;
        //}


        EditorBuildSettings.scenes = b.ToArray();

        AssetDatabase.SaveAssets();

        AssetDatabase.Refresh();

        Debug.Log("InitFramework success");
    }
}
